import express from 'express'
const router = express.Router()
import SuccessModel from '../module/SuccessModel.js'
import ErrorModel from '../module/ErrorModel.js'

import knex from '../utils/myKenx.js'

// 新增英雄接口
router.post('/message', (req, response) => {
  const { content, nickName } = req.body
  knex('message')
    .insert({ content, nickName })
    .then(res => {
      response.send(new SuccessModel({ msg: '新增成功' }))
    })
    .catch(err => {
      console.log(err)
      response.send(new ErrorModel({ msg: '新增失败' }))
    })
})

// // 详情接口
// (\\d+) 限制id只能是数字
router.get('/hero/:id(\\d+)', (req, response) => {
  // 获取id
  const { id } = req.params
  service
    .get(`https://game.gtimg.cn/images/lol/act/img/js/hero/${id}.js`, {
      method: 'get'
    })
    .then(res => {
      // console.log(res.data.skins)
      if (!res.data.skins) {
        // id有误
        response.send(new ErrorModel({ msg: 'id有误,请检查' }))
      } else {
        // 返回
        // 详情返回一条数据即可,不用返回数组
        response.send(new SuccessModel({ msg: '查询成功', data: res.data }))
        console.log(`英雄详情查询接口调用成功!` + new Date().toLocaleString())
      }
    })
})

// module.exports = router
export default router
